<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="hadd"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="list" border>
              <el-table-column label="序号" width="120" type="index" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template #default="{row}">
                  <el-button size="small" type="success" @click="hassign(row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="hedit(row)">编辑</el-button>
                  <el-button size="small" type="danger" @click="hdel(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination
                :current-page="pageForm.page"
                :page-sizes="[2, 4, 6, 8]"
                :page-size="pageForm.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
              />
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <!-- 新增弹框 -->
      <el-dialog
        :title="isEdit ? '编辑角色' :'添加角色'"
        :visible.sync="showDialog"
        @close="hclose"
      >
        <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small">取消</el-button>
            <el-button size="small" type="primary" @click="submit">确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>
      <!-- 分配权限弹框 -->
      <el-dialog title="分配权限" :visible.sync="showAssign">
        <assignPermission v-if="showAssign" :cur-id="curId" @close="showAssign=false" />
      </el-dialog>
    </div>
  </div>
</template>
<script>
import assignPermission from './assignPermission.vue'
import { getRole, addRole, delRole, updateRole } from '@/api/settings'
export default {
  components: {
    assignPermission
  },
  data() {
    return {
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入角色名称', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入角色介绍', trigger: 'blur' }
        ]
      },
      pageForm: {
        page: 1, // 页码值
        pagesize: 2// 每页条数
      },
      showAssign: false,
      list: [],
      total: 0,
      showDialog: false, //  弹框的显示和隐藏
      isEdit: false, // false 是添加 true是编辑
      curId: ''
    }
  },
  created() {
    this.loadRole()
  },
  methods: {
    // 分配权限
    async hassign(id) {
      this.showAssign = true
      this.curId = id
    },
    hclose() {
      // 重置验证规则
      this.$refs.roleForm.resetFields()
      this.roleForm = {
        name: '',
        description: ''
      }
    },
    // 删除
    async hdel(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        // 删除请求
        await delRole(id)
        // console.log(res)
        if (this.list.length === 1) {
          this.pageForm.page--
        }
        if (this.list.length <= 0) {
          this.pageForm.page === 1
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 点击确定按钮，调用函数发请求
    submit() {
      this.doaddRole()
      this.isEdit ? this.doEdit() : this.doaddRole()
    },
    // 编辑请求
    async doEdit() {
      await updateRole(this.roleForm.id)
      // console.log(res)
      this.loadRole()
    },
    // 编辑框显示
    async hedit(row) {
      // 想要获取el-table中每一项的数据，可以使用row，插件规定好的
      // 拿到row传过来 通过展开运算符 浅拷贝 赋值给 roleForm 不然会有，修改数据同时表格数据也在变化的情况
      this.roleForm = row
      // console.log(res)
      this.isEdit = true
      this.showDialog = true
    },
    // 新增请求
    async doaddRole() {
      await addRole(this.roleForm)
      // console.log(res)
      this.loadRole()
      this.showDialog = false
      this.total++
      this.pageForm.page = Math.ceil(this.total / this.pageForm.pagesize)
    },
    // 点击新增弹框显示
    async hadd() {
      this.isEdit = false
      this.showDialog = true
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`)
      this.pageForm.page = 1
      this.pageForm.pagesize = val
      this.loadRole()
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`)
      this.pageForm.page = val
      this.loadRole()
    },
    // 获取角色列表
    async loadRole() {
      const res = await getRole(this.pageForm)
      // console.log(res)
      this.list = res.data.rows
      this.total = res.data.total
    }
  }
}
</script>
